﻿using SuporeSliplampApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft;
using Newtonsoft.Json;
using System.IO;
using SuporeSliplampApp.Util;

namespace SuporeSliplampApp.Views
{
    /// <summary>
    /// OSDView.xaml 的交互逻辑
    /// </summary>
    public partial class OSDView : Window
    {

        public static event EventHandler<OSODModel> osodDelegate;

        private double selectedMultiplier;
        private int osod;

        public OSDView()
        {
            InitializeComponent();

            if (File.Exists("OSOD.json"))
            {
                string json = File.ReadAllText("OSOD.json");
                var osodModel = Newtonsoft.Json.JsonConvert.DeserializeObject<OSODModel>(json);
                if (osodModel != null)
                {
                    if (osodModel.Multiplier == 6)
                    {
                        Multiplier.SelectedIndex = 0;
                    }
                    else if (osodModel.Multiplier == 10)
                    {
                        Multiplier.SelectedIndex = 1;
                    }
                    else if (osodModel.Multiplier == 16)
                    {
                        Multiplier.SelectedIndex = 2;
                    }
                    else if (osodModel.Multiplier == 25)
                    {
                        Multiplier.SelectedIndex = 3;
                    }
                    else if (osodModel.Multiplier == 40)
                    {
                        Multiplier.SelectedIndex = 4;
                    }
                    else
                    {
                        Multiplier.SelectedIndex = 0;
                    }

                    if (osodModel.OSODindex == 1)
                    {
                        OSOD.SelectedIndex = 1;
                    }
                    else
                    {
                        OSOD.SelectedIndex = 0;
                    }

                }
            }

        }


        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void SaveBtn_Click(Object sender, RoutedEventArgs e)
        {
            osod = OSOD.SelectedIndex;

            if (Multiplier.SelectedItem is ComboBoxItem selectedItem)
            {
                string multiplierText = selectedItem.Content.ToString().Replace("x", "");
                if (double.TryParse(multiplierText, out double multiplier))
                {
                    selectedMultiplier = multiplier;
                }
            }

            OSODModel dt = new OSODModel();
            dt.Multiplier = selectedMultiplier;
            dt.OSODindex = osod;

            string json = Newtonsoft.Json.JsonConvert.SerializeObject(dt , Newtonsoft.Json.Formatting.Indented);
            File.WriteAllText("OSOD.json", json);

            osodDelegate?.Invoke(this, dt);

            Common.isselectedindex = 1;

            this.Close();
        }

    }
}
    